package com.tourlaid.mamagerment.ao.wx.impl

import com.tourlaid.mamagerment.ao.wx.ThoughtDetailAo
import com.tourlaid.mamagerment.service.CommentService
import com.tourlaid.mamagerment.service.RewardService
import com.tourlaid.mamagerment.service.ThoughtService
import com.tourlaid.mamagerment.service.UserService
import com.tourlaid.mamagerment.util.DateUtil
import com.tourlaid.mamagerment.util.LoginUtil
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.util.CollectionUtils
import org.springframework.util.StringUtils
import java.util.*
import javax.annotation.Resource

/**
 * Created by xiaoyongge on 2017/8/11.
 */
@Service("thoughtDetailAo")
class ThoughtDetailAoImpl : ThoughtDetailAo {

    @Resource
    val userService: UserService? = null
    @Resource
    val thoughtService: ThoughtService? = null
    @Resource
    val commentService: CommentService? = null
    @Resource
    val rewardService: RewardService? = null

    override fun getUserInfo(detailUserId: Long): MutableMap<String, Any?> {
        val user = userService!!.getById(detailUserId)
        var map = mutableMapOf("pic" to user.picture,
                "nickName" to user.nickName,
                "sex" to user.sex,
                "integral" to user.integral,
                "signature" to user.signature)
        return map
    }

    override fun getReward(thoughtId: Long): Int {
        var reward = 0;
        var rewardList = rewardService!!.queryByThoughtId(thoughtId)
        if (!CollectionUtils.isEmpty(rewardList)) {
            for (reward2 in rewardList) {
                reward = reward.plus(reward2.integral!!)
            }
        }
        return reward
    }

    override fun getThought(thoughtId: Long): MutableMap<String, Any?> {
        var thought = thoughtService!!.getById(thoughtId)
        var list: MutableList<String?> = mutableListOf()
        list.add(thought.imageOne)
        if (!StringUtils.isEmpty(thought.imageTwo)) {
            list.add(thought.imageTwo)
        }
        if (!StringUtils.isEmpty(thought.imageThree)) {
            list.add(thought.imageThree)
        }
        if (!StringUtils.isEmpty(thought.imageFour)) {
            list.add(thought.imageFour)
        }
        if (!StringUtils.isEmpty(thought.imageFive)) {
            list.add(thought.imageFive)
        }
        var time = "今天"
        val diff = DateUtil.getDistanceTime(Date(), thought.createTime)
        if (diff > 0L) {
            time = "$diff" + "天前"
        }
        var map = mutableMapOf("content" to thought.content,
                "list" to list,
                "createTime" to time)
        return map
    }

    override fun queryCommentsAll(toUserId: Long, thoughtId: Long): MutableList<MutableMap<String, String?>> {
        var list: MutableList<MutableMap<String, String?>> = mutableListOf()
        val dtoList = commentService!!.queryOtherCByThoughtId(toUserId, thoughtId);
        if (CollectionUtils.isEmpty(dtoList)) return list
        for (dto in dtoList) {
            var map = mutableMapOf(
                    "nickName" to dto.nickName,
                    "content" to dto.content
            )
            list.add(map)
        }
        return list
    }

    override fun queryWeComments(toUserId: Long, thoughtId: Long): MutableList<MutableMap<String, Any?>> {
        var list: MutableList<MutableMap<String, Any?>> = mutableListOf()
        val commentList = commentService!!.queryWeCByThoughtId(LoginUtil.userId!!, toUserId, thoughtId);
        if (CollectionUtils.isEmpty(commentList)) return list
        val userIdList = commentList.map { it -> it.fromUserId!! }
        val userList = userService!!.queryByIdList(userIdList)
        val userMap = userList.associate { Pair(it.id, it.nickName) }
        for (comment in commentList) {
            var map = mutableMapOf(
                    "id" to comment.id,
                    "fromUserId" to comment.fromUserId,
                    "fromName" to userMap.get(comment.fromUserId),
                    "toUser" to comment.toUserId,
                    "content" to comment.content
            )
            list.add(map)
        }
        return list
    }

    override fun publicComment(toUserId: Long?, thoughtId: Long?, content: String?) {
        commentService!!.publicComment(LoginUtil.userId, toUserId, thoughtId, content)
    }

    override fun deleteComment(thoughtId: Long, commentId: Long) {
        var thought = thoughtService!!.getById(thoughtId)
        commentService!!.deleteComment(thoughtId, thought.userId!!, commentId)
    }

    @Transactional
    override fun reward(thoughtId: Long) {
        val thought = thoughtService!!.getById(thoughtId)
        //打赏
        rewardService!!.reward(thought.userId, thoughtId)
        //扣自己积分
        userService!!.minusIntegral()
        //作者增加积分
        userService!!.plusIntegral(thought.userId, 1)
    }
}